package com.chare.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chare.mapper.AdminMapper;
import com.chare.mapper.MenuMapper;
import com.chare.pojo.Admin;
import com.chare.pojo.Menu;
import com.chare.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.security.Principal;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shi
 * @since 2022-06-17
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

@Autowired
private  MenuMapper menuMapper;
@Autowired
private AdminMapper adminMapper;
@Autowired
private RedisTemplate redisTemplate;
    @Override
    public List<Menu> getMenuByAdminName() {
        String key="Menu";
        Principal principal= SecurityContextHolder.getContext().getAuthentication();
        String name= principal.getName();
        Admin admin=adminMapper.selectOne( new QueryWrapper<Admin>().eq("username",name));
        Integer id=admin.getId();
       List<Menu> list=(List<Menu>) redisTemplate.opsForValue().get(key+id);
       if(CollectionUtils.isEmpty(list))
       {
          list= menuMapper.getMenuByAdminId(id);
           redisTemplate.opsForValue().set(key+id,list);
       }
        return list ;
    }

    @Override
    public List<Menu> getMenusWithRole() {
        return menuMapper.getMenusWithRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }
}
